package com.learning.platform.controller;

import com.learning.platform.dto.ApiResponse;
import com.learning.platform.dto.CourseResponse;
import com.learning.platform.security.UserPrincipal;
import com.learning.platform.service.CourseService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/teacher")
@PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
public class TeacherController {
    
    private final CourseService courseService;
    
    public TeacherController(CourseService courseService) {
        this.courseService = courseService;
    }
    
    @GetMapping("/courses")
    public ApiResponse<Page<CourseResponse>> getMyCourses(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            Authentication auth) {
        
        UserPrincipal principal = (UserPrincipal) auth.getPrincipal();
        Page<CourseResponse> courses = courseService.getTeacherCourses(principal.getUserId(), 
                PageRequest.of(page - 1, pageSize));
        return ApiResponse.success(courses);
    }
}










